package com.hifar.test.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.fw.utils.lang.StringUtils;
import com.hifar.sys.*;
import com.hifar.test.base.pojo.HfEnvConditions;
import com.hifar.test.base.pojo.VO.EnvConditionsQueryParamsVO;
import com.hifar.test.base.service.IHfEnvConditionsService;
import com.hifar.test.unit.pojo.HfEnvUnitImplement;
import com.hifar.test.unit.pojo.HfEnvUnitResult;
import com.hifar.test.unit.pojo.HfPrjUnitBase;
import com.hifar.test.unit.pojo.VO.HfEnvUnitImplementQueryParam;
import com.hifar.test.unit.pojo.VO.HfEnvUnitResultQueryParam;
import com.hifar.test.unit.pojo.VO.HfPrjUnitBaseQueryParam;
import com.hifar.test.unit.service.IHfEnvUnitImplementService;
import com.hifar.test.unit.service.IHfEnvUnitResultService;
import com.hifar.test.unit.service.IHfPrjUnitBaseService;
import com.hifar.test.unitclassify.pojo.HfPrjUnitClassify;
import com.hifar.test.unitclassify.pojo.HfPrjUnitClassifyAndUnitVo;
import com.hifar.test.unitclassify.service.IHfPrjUnitClassifyService;
import com.hifar.test.unitstandardrelation.pojo.HfPrjUnitStandard;
import com.hifar.test.unitstandardrelation.pojo.HfPrjUnitStandardVo;
import com.hifar.test.unitstandardrelation.pojo.VO.HfPrjUnitStandardQueryParamVO;
import com.hifar.test.unitstandardrelation.service.IHfPrjUnitStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/busi/test/api/unit-api")
@Api(tags = "试验项目API接口")
public class UnitApiController {
    @Autowired
    private IHfPrjUnitClassifyService hfPrjUnitClassifyService;
    @Autowired
    private IHfPrjUnitBaseService hfPrjUnitBaseService;

    @Autowired
    private IHfEnvUnitResultService unitResultService;
    @Autowired
    private IHfEnvUnitImplementService unitImplementService;

    @Autowired
    private IHfEnvConditionsService hfEnvConditionsService;

    @Autowired
    private IHfPrjUnitStandardService hfPrjUnitStandardService;

    @PostMapping(value = "/listAllClassifyAndUnitBase")
    @ApiOperation(value = "项目-检测项目-分类功能:查询列表(不带分页)")
    public BusiResponse listAllClassifyAndUnitBase(HttpServletRequest request) {
        Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
        String keyword = StringUtils.trimNull(pageInfo.get("keyword"));
        QueryWrapper<HfPrjUnitClassifyAndUnitVo> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("name", keyword);
        }
        queryWrapper.orderByAsc("row_sort");
        List<HfPrjUnitClassifyAndUnitVo> list = hfPrjUnitClassifyService.listHfPrjUnitClassifyAndUnitVo(queryWrapper);
        return BusiResponse.OK(list);
    }

    @PostMapping(value = "/classify-list")
    @ApiOperation(value = "项目-检测项目-分类功能:查询列表(不带分页)")
    public BusiResponse classifyList(HttpServletRequest request) {
        QueryWrapper<HfPrjUnitClassify> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag", "1");
        queryWrapper.orderByAsc("row_sort");
        queryWrapper.orderByDesc("create_time");
        List<HfPrjUnitClassify> list = hfPrjUnitClassifyService.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return BusiResponse.OK(list);
        } else {
            return BusiResponse.OK(new ArrayList<HfPrjUnitClassify>());
        }
    }

    @PostMapping(value = "/unit-list")
    @ApiOperation(value = "项目-检测项目功能:查询列表(不带分页)")
    public BusiResponse<List<HfPrjUnitBase>> listAll(HttpServletRequest request) {
        Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
        QueryWrapper<HfPrjUnitBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag", "1");
        String classifyId = StringUtils.trimNull(pageInfo.get("classifyId"));
        if (!"".equals(classifyId)) {
            queryWrapper.eq("classify_id", classifyId);
        }
        queryWrapper.orderByAsc("row_sort");
        queryWrapper.orderByDesc("create_time");
        List<HfPrjUnitBase> list = hfPrjUnitBaseService.list(queryWrapper);
        return BusiResponse.OK(list);
    }


    @PostMapping(value = "/unit-page")
    @ApiOperation(value = "项目-检测项目-分类功能:查询列表(分页)")
    public BusiResponse unitPage(@RequestBody HfPrjUnitBaseQueryParam queryParamVO) {
        String classifyId = queryParamVO.getClassifyId();
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamVO);
        queryParamBean.addColumns(HfPrjUnitBase.class);
        QueryWrapper queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        queryWrapper.ne("del_flag", "1");
        if (StringUtils.isNotBlank(classifyId)) {
            queryWrapper.eq("classify_id", classifyId);
        }
        queryWrapper.orderByAsc("row_sort");
        queryWrapper.orderByDesc("create_time");
        Page pageData = hfPrjUnitBaseService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @GetMapping("/queryById")
    @ApiOperation(value = "项目-检测项目功能:根据id查询数据")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfPrjUnitBase byId = hfPrjUnitBaseService.getById(id);
        // 查询转换附件
        return BusiResponse.OK(byId);
    }

    @PostMapping(value = "/standard-page")
    @ApiOperation(value = "项目-检测项目-标准功能:分页查询列表")
    public BusiResponse standardPage(@RequestBody @Validated HfPrjUnitStandardQueryParamVO queryParamVO) {
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamVO);
        queryParamBean.addColumns(HfPrjUnitStandard.class);
        queryParamBean.addField("standardCode");
        queryParamBean.addField("standardName");
        QueryWrapper queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        queryWrapper.ne("del_flag", "1");
        String unitId = queryParamVO.getUnitId();
        if (StringUtils.isNotBlank(unitId)) {
            queryWrapper.eq("unit_id", unitId);
        }
        queryWrapper.orderByDesc("create_time");
        Page<HfPrjUnitStandardVo> pageData = hfPrjUnitStandardService.pageHfPrjUnitStandardVo(page, queryWrapper);
        /*计算分页信息返回给前端*/
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);

    }


    @PostMapping(value = "/implement-page")
    @ApiOperation(value = "项目试验实施:查询全部")
    public BusiResponse implementPage(@RequestBody @Validated HfEnvUnitImplementQueryParam queryParamsVO) {
        QueryParamBean queryParamBean = new QueryParamBean(queryParamsVO, HfEnvUnitImplement.class);
        QueryWrapper<HfEnvUnitImplement> queryWrapper = queryParamBean.getQueryWrapper();
        String unitId = queryParamsVO.getUnitId();
        queryWrapper.lambda()
                .eq(HfEnvUnitImplement::getUnitId, unitId);
        Page<HfEnvUnitImplement> page = queryParamBean.getPage();
        Page<HfEnvUnitImplement> pageData = unitImplementService.page(page, queryWrapper);
        /* 计算分页信息返回给前端 */
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

    @PostMapping(value = "/result-page")
    @ApiOperation(value = "项目试验结论:分页查询列表")
    public BusiResponse resultPage(@RequestBody @Validated HfEnvUnitResultQueryParam queryParamsVO) {
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvUnitResult.class);
        QueryWrapper<HfEnvUnitResult> queryWrapper = queryParamBean.getQueryWrapper();
        String unitId = queryParamsVO.getUnitId();
        queryWrapper.lambda()
                .eq(HfEnvUnitResult::getUnitId, unitId);
        Page<HfEnvUnitResult> page = queryParamBean.getPage();
        Page<HfEnvUnitResult> pageData = unitResultService.page(page, queryWrapper);
        /* 计算分页信息返回给前端 */
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

    @PostMapping("/conditions-page")
    public BusiResponse conditionsPage(@RequestBody @Validated EnvConditionsQueryParamsVO queryParamsVO) {
        QueryParamBean<HfEnvConditions> queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvConditions.class);
        QueryWrapper<HfEnvConditions> queryWrapper = queryParamBean.getQueryWrapper();
        Page page = queryParamBean.getPage();
        String unitId = queryParamsVO.getUnitId();
        queryWrapper.lambda()
                .eq(HfEnvConditions::getUnitId, unitId);
        Page pageData = hfEnvConditionsService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }
}
